import { useState } from "react"
import apiV1 from "@/apis"
import { toast } from "react-toastify"

interface WithdrawParams {
    amount: string
    accountId?: string
}

export default () => {
    const [loading, setLoading] = useState(false)

    const withdraw = async (params: WithdrawParams) => {
        try {
            setLoading(true)
            const res = await apiV1.v1.refGetFinancePaymentWithdraw(params)

            if (res.data.status) {
                toast.success(
                    `Withdrawal request successful. The amount of ${params.amount} will be credited to your account after approval. Please stay tuned.`,
                )
                return true
            }
            return false
        } catch (error) {
            toast.error(error instanceof Error ? error.message : String(error))
            return false
        } finally {
            setLoading(false)
        }
    }

    return {
        withdraw,
        loading,
    }
}
